//
//  SetAddressViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/14.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "SetAddressViewController.h"

@interface SetAddressViewController ()<UITextFieldDelegate>


@end

@implementation SetAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"地址设置" fontSize:18];
       
    [self getAddressrequestData];
    
    [self.view makeToast:@"地址暂时不能修改"];
    self.address.delegate=self;
    self.distance.delegate=self;
    self.view.userInteractionEnabled=NO;
    
    
    [self addLeftBackBtn:nil];

    // Do any additional setup after loading the view from its nib.
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    __weak typeof(self) weakSelf = self;
    
    
    [self setRightTextBarButtonItemWithFrame:CGRectMake(0, 0, 60, 20) title:@"完成" titleColor:UIColorFromRGB(0x00ccc2) backImage:nil selectBackImage:nil action:^(WRBButton *button) {
        [weakSelf setAddressResuest:button];
        
    }];

    return YES;
    
}
-(void)setAddressResuest:(WRBButton *)btn
{
    if (self.address.text.length>1&&self.distance.text.length>0) {
        [self setAddressrequestData];

    }
    else{
        if (self.distance.text.length<1) {
            [self showFailed:@"请设置距离"];
            
        }
       else if (self.address.text.length<2) {
           [self showFailed:@"请设置不少于两个字的地址"];

        }
        else{
            [self showFailed:@"未知错误"];

        }

        
    }
    
    
    
    
    
}
-(void)getAddressrequestData
{
    
    
    [self addProgressHUD];
    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[self getArtisanId], @"artisanId", nil];
    
    NSLog(@"订单详情参数：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/artisan/getArtisanAddress" parameters:dictionary option:RequestCenterCachePolicyCacheAndLocal sucess:^(id responseObject) {
              [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            
            
            NSDictionary *dic=responseObject[@"address"];
            
            self.address.text=dic[@"address"];
            self.distance.text=[NSString stringWithFormat:@"%@",dic[@"distance"]];

        }
        else{
            
            [self showFailed:responseObject[@"message"]];
            
        }
        
        
    } failur:^(NSError *error) {
            [self removeStatuslable];
        
    }];
    
}



-(void)setAddressrequestData
{
    
    
    [self addProgressHUD];
    

    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:[self getArtisanId], @"artisanId", self.distance.text,@"distance",self.address.text,@"address",nil];

    
    NSLog(@"设置地址：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/artisan/setArtisanAddress" parameters:dictionary option:RequestCenterCachePolicyCacheAndLocal sucess:^(id responseObject) {
        [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            
            [self showSuccess:@"设置成功"];
            
        }
        else{
            
            [self showFailed:responseObject[@"message"]];
            
        }
        
        
    } failur:^(NSError *error) {
        [self removeStatuslable];
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
